package kr.hkit.powerjava.chapter26.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

import kr.hkit.powerjava.chapter26.dao.DepartmentDAO;
import kr.hkit.powerjava.chapter26.dto.Department;

public class DepartmentService extends AbsService<Department> {
	
	//연결은 여기서
	public DepartmentService(Scanner sc, Connection connection) {
		super(sc, connection);
		dao = new DepartmentDAO(connection); //dao생성은 여기서
	}

	@Override
	public Department inputItemInfo(boolean gubun) {
		System.out.println("부서번호 부서명");
		
		Department dept = new Department(sc.nextInt(), sc.next().trim()); //trim은 양사이드 공백제거
		
		if (gubun) {
			System.out.println("위치");
			int floor = sc.nextInt();
			dept.setFloor(floor);
		}
		return dept;
	}

	@Override
	public void prnSubMenu() {
		
		ArrayList<Department> list = null;
		
		System.out.println("1. 이름순 정렬 2. 위치별 정렬 3. 번호별 정렬");
		try {
			list = dao.listItems();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		int res = sc.nextInt(); //정렬용
		switch (res) {
			case 1:
				prnItemList(new Comparator<Department>() {					
					@Override
					public int compare(Department o1, Department o2) {
						return o1.getDeptName().hashCode()-o2.getDeptName().hashCode();
					}
				}, list);
				break;
			case 2:
				prnItemList(new Comparator<Department>() {
					@Override
					public int compare(Department o1, Department o2) {
						return o1.getFloor()-o2.getFloor();
					}
				}, list);
				break;
			default:
				prnItemList(null, list);
		}		
	}

	/*//정렬구현용.
	//employee에서도 쓰고 department에서도 쓰니까 abs서비스에 추가!!! 
	private void prnItemList(Comparator<Department> comparator) { 
		if (comparator == null) {
			Collections.sort(list);
		}else{
			Collections.sort(list,comparator);
		}
		prnlist(list); //정렬내용 출력용
	}

	private void prnlist(ArrayList<Department> list) { //정렬내용 출력용
		for (Department d : list) {
			System.out.println(d);
		}
	}*/
}
